<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // generator函数是一个状态机（迭代生成器，封装了多个内部状态，返回值是一个迭代器（iterator)
        // generator可以用于 异步调用  

        // 语法
        function* gen() {
            console.log("generator", 1);
            yield "a";
            console.log("generator", 2);
            return "b"
        }
        let it = gen();
        console.log(it);
        console.log(it.next())
        console.log(it.next())
        console.log(it.next())


        {
            //yield 1 ==> next()返回值
            //let a = yield  ==> next(1) 传参
            function* gen() {
                console.log("generator", 1);
                let a = yield "666";
                console.log("generator", a); //20
                let b = yield "888";
                console.log("generator", b);
            }
            let it = gen();
            console.log(it.next(10)); //{value: "666", done: false} //第一个next的传参没有任何意义
            console.log(it.next(20)); //{value: "888", done: false}
            console.log(it.next(30));
        }



    </script>
</body>

</html>